<template>
  <div class="login">
    <div class="box">
      <div class="title">用户登录</div>
      <div class="input">
        <label
          >账户:
          <el-input v-model="username"></el-input>
        </label>
      </div>

      <div class="input">
        <label
          >密码:
          <el-input v-model="PWD" show-password></el-input>
        </label>
      </div>
      <el-button type="primary" @click="login">登录</el-button>
    </div>
  </div>
</template>

<script>
import { loginAxios } from "../http/apis/userManageApi";
export default {
  data() {
    return {
      username: "",
      PWD: "",
    };
  },
  methods: {
    login() {
      if (this.username === "" || this.PWD === "") {
        this.$message.error("账号或密码不能为空");
      } else {
        loginAxios({
          username: this.username,
          PWD: this.PWD,
        })
          .then((res) => {
            if (res.data.code === 200) {
              console.log("token", res);
              sessionStorage.setItem("isLogin", true);
              sessionStorage.setItem("token", res.data.token);
              const wantUrl = this.$route.query.redirect;
              this.$router.push(wantUrl ? wantUrl : "/");
            } else {
              this.$message.error("账号或密码错误");
            }
          })
          .catch(() => {
            this.$message.error("登陆失败");
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 500px;
  height: 400px;
  margin: 100px auto;
  border: 1px solid #ccc;
  box-shadow: 0 0 7px #ccc;
  .title {
    width: 80px;
    height: 30px;
    padding: 5px;
    margin: 20px auto 0;
    line-height: 30px;
    text-align: center;
    font-weight: 600;
  }
  .input {
    width: 300px;
    margin: 0 auto;
    .el-input {
      width: 200px;
    }
    label {
      width: 260px;
      display: block;
      margin: 30px auto;
    }
  }
}

.el-button {
  display: block;
  width: 100px;
  margin: 20px auto;
}
</style>